/*
 * @Author: your name
 * @Date: 2020-09-25 10:24:39
 * @LastEditTime: 2020-09-25 17:41:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \jqWebpack\config\entry.js
 */
const path = require('path')
// const { join, resolve } = path
const fs = require('fs')
 // getEntry方法用于获取多页面entry入口，包括{ 入口名， 入口路径, 输出html文件模板路径 }
let entryList = []
function getEntry(entry, jsPath, filePath) {
    console.log('entry', entry)
    console.log('jsPath', jsPath)
     if(!entry || !jsPath) {
         throw(new Error('请添加入口地址'))
     }

     let BasePath = path.resolve(__dirname, entry) // 获取页面文件更路径
     console.log('查看path', BasePath)
     let Files = fs.readdirSync(BasePath)
     console.log('Files', Files)
     if(!filePath) {
        filePath = ''
     }
     for(let i=0; i<Files.length; i++) {
         if (/.html$/.test(Files[i])) {
            const buildPath = Files[i].split('.html')[0]
            const outPath = filePath + Files[i].split('.html')[0]
            const entryName = joinPath(outPath)
            const outputHtml = path.join(BasePath, Files[i])
            console.log('查看jsPath', jsPath)
            const entryPath = path.join(path.resolve(__dirname, jsPath + (filePath ? `/${filePath}` : '')), buildPath) + '.js'
            entryList.push({name: entryName, outPath, outputHtml: outputHtml, entryPath, fileName:buildPath  })
         } else {
            let stat = fs.lstatSync(path.join(BasePath, Files[i]));
            let is_direc = stat.isDirectory(); // 判断是否是文件夹
            if (is_direc) {
                const NewEntry = entry + `/${Files[i]}`
                getEntry(NewEntry, jsPath, filePath + `${Files[i]}/`)
            }
         }
     }
     return entryList
 }
 function joinPath(file) {
    const data = file.split('/')
    return data.filter((item) => item).join('-')
 }

 module.exports =  getEntry